//
//  main.m
//  05-排序、数组字典的处理
//
//  Created by LL.F on 16/7/8.
//  Copyright © 2016年 LL.F. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Person.h"
#import "TempCity.h"
#import "City.h"
#import "Zone.h"
#import "Province.h"
int main(int argc, const char * argv[]) {

    // 一、
    // 1. 创建1个包含5个字符串元素的数组；
    // 2. 用for循环倒序输出所有元素；
    // 3. 用for...in完成所有字符串元素的拼接(生成一个新的字符串)，并输出拼接好的字符串。
    
    NSArray *stringArray = @[@"大哥", @"二哥", @"三哥", @"四哥", @"五哥"];
    for (NSInteger i = stringArray.count - 1; i >= 0; i--) {
        NSLog(@"%@", stringArray[i]);
    }
    
    // 二、
    // 1. 创建1个包含5个person对象的数组(姓名用英文)；
    // 2. 用for循环找到年龄最大的Person对象；
    // 3. 用for...in遍历数组，把大于某个年龄的Person对象的姓名后面拼接上字符串“-brother”。
    
    Person *per1 = [Person personWithName:@"Jason" age:18 gender:@"男"];
    Person *per2 = [Person personWithName:@"Jack" age:20 gender:@"男"];
    Person *per3 = [Person personWithName:@"Dany" age:5 gender:@"女"];
    Person *per4 = [Person personWithName:@"Smith" age:23 gender:@"男"];
    Person *per5 = [Person personWithName:@"Lucase" age:21 gender:@"女"];
    NSArray *personArray = @[per1, per2, per3, per4, per5];
    int age = 0;
    NSInteger index = 0;
    for (NSInteger i = 0; i < personArray.count; i++) {
        if ([personArray[i] age] > age) {
            age = (int)[personArray[i] age];
            index = i;
        }
    }
    NSLog(@"%@", personArray[index]);//.m里重写了description方法
    
    // 三、
    // 1. 使用第二题目中生成的数组；
    // 2. 使用sortUsingSelector: 分别按年龄，姓名，性别进行排序，输出排序结果；
    // 3. 使用NSSortDescriptor，按姓名进行排序。
    
    // 使用sortUsingSelector
    personArray = [personArray sortedArrayUsingSelector:@selector(compareByAge:)];
    NSLog(@"%@", personArray);
    personArray = [personArray sortedArrayUsingSelector:@selector(compareByName:)];
    NSLog(@"%@", personArray);
    personArray = [personArray sortedArrayUsingSelector:@selector(compareByGender:)];
    NSLog(@"%@", personArray);
    
    // 使用NSSortDescriptor
    NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    personArray = [personArray sortedArrayUsingDescriptors:@[descriptor]];
    NSLog(@"%@", personArray);
    
    // 四、
    // 处理省市区文件中的area.txt，将省市区进行分割处理, 省里包含市, 市里包含区
    // 1. 先用数组字典表示
    // 2. 将数组字典的处理方式转换成数组模型(Class)
    
    //1.
    NSString *path = @"/Users/LL.F/DLS160710/Objective-C/HomeWork/05-排序、数组字典的处理（数据解析）/05-排序、数组字典的处理/area.txt";
    // 拿到数据源字符串
    NSString *sourceString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    //NSLog(@"%@", sourceString);
    // 根据回车分割字符串
    NSArray *allArray = [sourceString componentsSeparatedByString:@"\n"];
    // 总体的大数组
    NSMutableArray *allProvinceArray = [NSMutableArray array];
    //NSLog(@"%@", allArray);
    for (NSString *area in allArray) {
        // 取到所有的省(没有空格开头)
        if (![area hasPrefix:@" "]) {
            // 创建了省字典（字典中包含了两个字段 name array）
            NSMutableDictionary *proDic = [NSMutableDictionary dictionary];
            // 添加ProName字段
            [proDic setObject:area forKey:@"proName"];
            // 创建了省中所有市的数组
            NSMutableArray *cityArray = [NSMutableArray array];
            // 添加数组字段
            [proDic setObject:cityArray forKey:@"cityArray"];
            // 将创建好的省字典放到省数组中
            [allProvinceArray addObject:proDic];
            // NSLog(@"%@", proDic);
            //取到所有的市（两个空格开头，但不是四个空格开头）
        } else if (![area hasPrefix:@"    "]) {
            // 通过lastObject获取到对应的省字典
            NSMutableDictionary *prodic = [allProvinceArray lastObject];
            // 拿到省字典中对应的市数组
            NSMutableArray *cityArray = [prodic objectForKey:@"cityArray"];
            // 创建市字典（name和区array）
            NSMutableDictionary *cityDic = [NSMutableDictionary dictionary];
            [cityDic setObject:area forKey:@"cityName"];
            NSMutableArray *zoneArray = [NSMutableArray array];
            [cityDic setObject:zoneArray forKey:@"zoneArray"];
            // 向市数组中添加市字典
            [cityArray addObject:cityDic];
            // 取到所有的区、县
        } else {
            // 通过省数组获取到当前所在的省字典
            NSMutableDictionary *proDic = [allProvinceArray lastObject];
            // 通过省字典获取到市数组
            NSMutableArray *cityArray = [proDic objectForKey:@"cityArray"];
            // 通过市数组获取到对应的市字典
            NSMutableDictionary *cityDic = [cityArray lastObject];
            // 通过市字典获取到对应的区、县数组
            NSMutableArray *zoneArray = [cityDic objectForKey:@"zoneArray"];
            // 最终将区县添加到区、县数组中
            [zoneArray addObject:area];
        }
    }
    NSLog(@"%@", allProvinceArray);
    
    //2.
    NSMutableArray *allProArray = [NSMutableArray array];
    for (NSString *area in allArray) {
        if (![area hasPrefix:@" "]) {
            Province *pro = [Province zoneWithName:area];
            [allProArray addObject:pro];
        } else if ([area hasPrefix:@"  "] && ![area hasPrefix:@"    "]) {
            Province *pro = [allProArray lastObject];
            City *city = [City zoneWithName:@"area"];
            [pro.array addObject:city];
        } else {
            City *city = [[[allProArray lastObject] array] lastObject];
            Zone *zone = [Zone zoneWithName:area];
            [city.array addObject:zone];
        }
    }

    // 五、拓展
    // 上述作业做完, 处理Location1.txt要求:
    // 1. 先用数组字典表示
    // 2. 将数组字典的处理方式转换成数组模型(Class)
    
    
    //1.
    NSString *pathString = @"/Users/LL.F/DLS160710/Objective-C/HomeWork/05-排序、数组字典的处理（数据解析）/05-排序、数组字典的处理/Location1.txt";
    NSString *source = [NSString stringWithContentsOfFile:pathString encoding:NSUTF8StringEncoding error:nil];
    // 根据逗号分割字符串，得到所有城市的数组
    NSArray *allCityArray = [source componentsSeparatedByString:@","];
    //NSLog(@"%@", allCityArray);
    NSMutableArray *allCityDicArray = [NSMutableArray array];
    // 根据每个城市中的| 处理字符串， 重新生成字典数组
    for (NSString *city in allCityArray) {
        // 根据切割生成的数组，生成新的城市字典
        NSArray *arrayCity = [city componentsSeparatedByString:@"|"];
        NSMutableDictionary *cityDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:[arrayCity firstObject], @"cityName", [arrayCity lastObject], @"cityNumber", nil];
        // 添加到新的数组中
        [allCityDicArray addObject:cityDic];
        
    }
    // 排序（很重要）
    NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"cityNumber" ascending:YES];
    [allCityDicArray sortUsingDescriptors:@[sort]];
    //NSLog(@"%@", allCityDicArray);
    NSMutableArray *finalArray = [NSMutableArray array];
    for (NSMutableDictionary *cityDic in allCityDicArray) {
        NSString *cityName = cityDic[@"cityName"];
        NSString *cityNumber = cityDic[@"cityNumber"];
        if ([cityNumber hasSuffix:@"0000"]) {
            NSMutableDictionary *proDic = [NSMutableDictionary dictionaryWithDictionary:cityDic];
            [proDic setObject:[NSMutableArray array] forKey:@"cityArray"];
            [finalArray addObject:proDic];
            //NSLog(@"省");
        } else if ([cityNumber hasSuffix:@"00"] && ![cityNumber hasSuffix:@"0000"]) {
            NSMutableDictionary *proDic = [finalArray lastObject];
            NSMutableArray *cityArray = [proDic objectForKey:@"cityArray"];
            NSMutableDictionary *dicCity = [NSMutableDictionary dictionaryWithDictionary:cityDic];
            [dicCity setObject:[NSMutableArray array] forKey:@"zoneArray"];
            [cityArray addObject:dicCity];
            
            NSLog(@"市");
        } else {
            
            //NSMutableArray *zoneArray = [[[[finalArray lastObject]objectForKey:@"cityArray"] lastObject]objectForKey:@"zoneArray"];(复杂式子简单化如下)
            
            NSMutableDictionary *proDic = [finalArray lastObject];
            NSMutableArray *cityArray = [proDic objectForKey:@"cityArray"];
            NSMutableDictionary *dicCity = [cityArray lastObject];
            NSMutableArray *zoneArray = [dicCity objectForKey:@"zoneArray"];
            NSMutableDictionary *zoneDic = [NSMutableDictionary dictionaryWithDictionary:cityDic];
            [zoneArray addObject:zoneDic];
            //NSLog(@"区");
        }
    }
    NSLog(@"%@", finalArray);
    
    //2.
    NSMutableArray *allTempCityArray = [NSMutableArray array];
    for (NSString *cityString in allCityArray) {
        //根据|切割字符串
        NSArray *city = [cityString componentsSeparatedByString:@"|"];
        TempCity *tempCity = [[TempCity alloc] initWithName:[city firstObject] number:[city lastObject]];
        [allTempCityArray addObject:tempCity];
    }
    //对类数组排序
    NSSortDescriptor *sortTempCity = [[NSSortDescriptor alloc] initWithKey:@"cityName" ascending:YES];
    [allTempCityArray sortUsingDescriptors:@[sortTempCity]];
    //NSLog(@"%@", allTempCityArray);
    NSMutableArray *finalClassArray = [NSMutableArray array];
    for (TempCity *temp in allTempCityArray) {
        if ([temp.cityNumber hasSuffix:@"0000"]) {
            Province *pro = [Province zoneWithTempCity:temp];
            [finalClassArray addObject:pro];
        } else if ([temp.cityNumber hasSuffix:@"00"] && ![temp.cityNumber hasSuffix:@"0000"]) {
            Province *pro = [finalClassArray lastObject];
            City *city = [City zoneWithTempCity:temp];
            [pro.array addObject:city];
        } else {
            Province *pro = [finalClassArray lastObject];
            City *city = [pro.array lastObject];
            Zone *zone = [Zone zoneWithTempCity:temp];
            [city.array addObject:zone];
        }
    }
    //NSLog(@"%@", finalClassArray);
    return 0;
}
